﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Meditime.Domain;
using readmodel = Meditime.UI;

namespace Meditime.UI
{
    public static class EventHandlers
    {
        public static void HandleEvent(StaffMemberCreated @event)
        {
            using (ReadModelEntities readModelDb = new ReadModelEntities())
            {
                readModelDb.StaffMembers.AddObject(
                    readmodel.StaffMember.CreateStaffMember(
                        @event.StaffMemberId, @event.Name, @event.DateOfBirth, @event.NextState, @event.Version)
                    );
                readModelDb.SaveChanges();
            }
        }

        public static void HandleEvent(MadeContract @event)
        {
            using (ReadModelEntities readModelDb = new ReadModelEntities())
            {
                var member = (from m in readModelDb.StaffMembers
                              where m.Id == @event.StaffMemberId
                              select m).FirstOrDefault();
                member.Status = @event.NextState;
                member.Version = @event.Version;
                readModelDb.SaveChanges();
            }
        }

        public static void HandleEvent(MadeFulltime @event)
        {
            using (ReadModelEntities readModelDb = new ReadModelEntities())
            {
                var member = (from m in readModelDb.StaffMembers
                              where m.Id == @event.StaffMemberId
                              select m).FirstOrDefault();
                member.Status = @event.NextState;
                member.Version = @event.Version;
                readModelDb.SaveChanges();
            }
        }

        public static void HandleEvent(MarkedAsInactive @event)
        {
            using (ReadModelEntities readModelDb = new ReadModelEntities())
            {
                var member = (from m in readModelDb.StaffMembers
                              where m.Id == @event.StaffMemberId
                              select m).FirstOrDefault();
                readModelDb.StaffMembers.DeleteObject(member);
                readModelDb.SaveChanges();
            }
        }

        public static void HandleEvent(NameUpdated @event)
        {
            using (ReadModelEntities readModelDb = new ReadModelEntities())
            {
                var member = (from m in readModelDb.StaffMembers
                              where m.Id == @event.StaffMemberId
                              select m).FirstOrDefault();
                member.Name = @event.Name;
                member.Version = @event.Version;
                readModelDb.SaveChanges();
            }
        }

        public static void HandleEvent(DateOfBirthUpdated @event)
        {
            using (ReadModelEntities readModelDb = new ReadModelEntities())
            {
                var member = (from m in readModelDb.StaffMembers
                              where m.Id == @event.StaffMemberId
                              select m).FirstOrDefault();
                member.DateOfBirth = @event.DateOfBirth;
                member.Version = @event.Version;
                readModelDb.SaveChanges();
            }
        }


        public static void HandleEvent(dynamic @event)
        {

            AddedToRoster currentEvent = @event as AddedToRoster;

            using (ReadModelEntities readModelDb = new ReadModelEntities())
            {
                Guid memberId = @event.StaffMemberId;

                var memberName = (from n in readModelDb.StaffMembers
                                  where n.Id == memberId
                                  select n.Name).FirstOrDefault();

                readModelDb.ShiftTimes.AddObject(
                        readmodel.ShiftTime.CreateShiftTime(
                                @event.EventId, @event.ShiftRecordingId, @event.StaffMemberId, 
                                memberName, @event.NextState, @event.ShiftStart, @event.Version)
                    );

                var currentStatus = (from s in readModelDb.CurrentShiftStatuses
                                     where s.StaffMemberId == memberId
                                     select s).FirstOrDefault();
                if (currentStatus == null)
                {
                    readModelDb.CurrentShiftStatuses.AddObject(
                        readmodel.CurrentShiftStatus.CreateCurrentShiftStatus(
                            @event.StaffMemberId, memberName, @event.NextState, @event.ShiftStart, @event.ShiftRecordingId, @event.Version));
                }
                else
                {
                    currentStatus.CurrentShiftState = @event.NextState;
                    currentStatus.StartTime = @event.ShiftStart;
                    currentStatus.VersionNo = @event.Version;
                }

                readModelDb.SaveChanges();
            }
        }
    }
}
